package com.tansun.rule.sessionPool;

import org.kie.api.runtime.KieSession;

public class SessionPool {

//	private static int MaxTotal = 20;
//	private static int MinTotal = 5;
//	private GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
	
//	private GenericObjectPool<KieSession> pool = null;
	
//	public SessionPool(int maxTotal,int maxIdle, PooledSessionFactory pooledSessionFactory){
//		conf.setMaxTotal(maxTotal);
//		conf.setMaxIdle(maxIdle);
//		PooledSessionFactory psf = new PooledSessionFactory();
//		pool = new GenericObjectPool<KieSession>(pooledSessionFactory, conf);
//	}
//	public SessionPool(String ruleProject, PooledSessionFactory pooledSessionFactory){
//		int maxTotal = AppConfiguration.RuleProjMap.get(ruleProject).getSessionMaxTotal();
//		int maxIdle = AppConfiguration.RuleProjMap.get(ruleProject).getSessionMaxIdle();
//		conf.setMaxTotal(maxTotal);
//		conf.setMaxIdle(maxIdle);
//		pool = new GenericObjectPool<KieSession>(pooledSessionFactory, conf);
//	}
	
//	public KieSession borrowKieSession(){
//		KieSession kieSession=null;
//		try {
//			kieSession = pool.borrowObject();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return kieSession;
//	}
//	public void returnKieSession(KieSession kieSession) {
//		pool.returnObject(kieSession);
//	}
	
//	public boolean clearPool(){
//		pool.clear();
//		return true;
//	}
	private PooledSessionFactory pool = null;

	public SessionPool(PooledSessionFactory pooledSessionFactory) {
		pool = pooledSessionFactory;
	}

	public KieSession borrowKieSession() throws Exception {
		return pool.create();
	}

	public void returnKieSession(KieSession kieSession) {
		kieSession.destroy();
		kieSession = null;
	}
}
